package com.yh.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;


import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.BBS;
import com.yh.bean.BBSUserShow;
import com.yh.bean.Message;
import com.yh.bean.ReplyBBS;
import com.yh.bean.User;
import com.yh.service.BBSService;

@Controller
public class BBSController {
	
	@Autowired
	@Qualifier("bbs")
	private BBSService service;
	
	/*
	 * 查询论坛信息
	 */
	@RequestMapping("querybbs")
	@ResponseBody
	public Map queryBBS(Integer curr,BBS bbs, HttpSession req){
		
		if(curr == null ){
			curr=1;
		}
		System.out.println(bbs);
		Map map=new HashMap();
		//使用layPage来设置分页格式	在格式后边加入"字段"可以通过字段来排序
		Page page=PageHelper.startPage(curr,4,"bbs_id desc");
		List<BBS> blist=service.querybbs(bbs);
		//total和mlist存贮到map集合中 发送到前台页面
		map.put("total", page.getTotal());	//获取总条数
		map.put("mlist", blist);
		return map;
	}
	
	/*
	 * 添加主贴
	 */
	@RequestMapping("insertbbs")
	@ResponseBody
	public int insertBBS(BBS bbs, HttpSession req){
		//从页面session中获取当前用户id,name
		int bbs_userid=(int) req.getAttribute("bbs_userid");
		String bbs_name=(String) req.getAttribute("bbs_name");
		//获取当前时间添加到bbs对象中
		Date bbs_time=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年M月d日H时m分");
		//获取到的id,name,time添加到bbs对象中
		bbs.setBbs_time(sdf.format(bbs_time));
		bbs.setBbs_userid(bbs_userid);
		bbs.setBbs_name(bbs_name);
		//创建返回对象
		int a =service.insertbbs(bbs);
		return a;
	}
	
	/*
	 * 删除帖子信息
	 */
	@RequestMapping("/deletebbs")
	@ResponseBody
	public int deletebbs(int bid){
		System.out.println(bid);
		int a=service.deletebbs(bid);
		return a;
	}
	
	/*
	 * 进入帖子显示详细信息
	 */
	@RequestMapping("/showbbs")
	public String showbbs(int bid,Model model){
		BBS bbs=service.showbbs(bid);
		model.addAttribute("bbs",bbs);
		return "showbbs";
	}
	
	/*
	 * 查询回复内容
	 */
	@RequestMapping("/queryReply")
	@ResponseBody
	public Map queryReply(int rebbs_bid,int curr){
		System.out.println("输出测试bid"+rebbs_bid);
		System.out.println("输出测试curr"+curr);
		Map map=new HashMap();
		//分页设置
		List<ReplyBBS> reply1=service.queryReply(rebbs_bid);
		System.out.println("contro层输出测试"+reply1);
		Page page=PageHelper.startPage(curr,5,"rebbs_id");	
		List<ReplyBBS> reply=service.queryReply(rebbs_bid);
		map.put("reply", reply);
		map.put("total", page.getTotal());	//获取总条数
		map.put("pages", page.getPages());	//多少页
		return map;
	}
	
	/*
	 * 添加留言
	 */
	@RequestMapping("/addReply")
	@ResponseBody
	public int insertRebbs(ReplyBBS reply,HttpSession req){
		System.out.println("添加测试"+reply);
		//获取session对象中的name和id添加到reply对象中
		String rebbs_name= (String) req.getAttribute("bbs_name");
		int rebbs_userid=(int) req.getAttribute("bbs_userid");
		//获取当前时间
		Date rebbs_time=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy年M月d日H时m分");
		//时间，name,id添加到reply对象中
		reply.setRebbs_userid(rebbs_userid);
		reply.setRebbs_name(rebbs_name);
		reply.setRebbs_time(sdf.format(rebbs_time));
		System.out.println("impl前"+reply);
		int a =service.insertRebbs(reply);
		System.out.println(a+"添加调用后测试"+reply);
		return a;
	}
	
	/*
	 * 删除信息
	 */
	@RequestMapping("/deleteReply")
	@ResponseBody
	public int deleteReply(int reid){
		System.out.println("输出测试"+reid);
		int a=service.deleteReply(reid);
		System.out.println("输出测试"+a);
		return a;
	}
	
	/*
	 * 跳转用户中转
	 */
	@RequestMapping("/showUser")
	public String showUser(int userid,Model model){
		model.addAttribute("userid", userid);
		System.out.println("测试model"+model);
		return "showUser";
	}
	
	/*
	 * 查询贴吧用户详情表
	 * 使用4表联查--用户表，帖子表，回帖表，部门表
	 */
	@RequestMapping("/queryUserBBS")
	@ResponseBody
	public Map queryUserBBS(int userid,int curr){
		Map map=new HashMap();
		Page page=PageHelper.startPage(curr,4,"bbs_id desc");
		List<BBS> blist=service.queryUserBBS(userid);
		map.put("total", page.getTotal());	//获取总条数
		map.put("blist", blist);
		System.out.println(map);
		return map;
	}
	
	@RequestMapping("/queryUserReply")
	@ResponseBody
	public Map queryUserReply(int userid,int curr){
		Map map=new HashMap();
		Page page=PageHelper.startPage(curr,4,"rebbs_time desc");
		List<ReplyBBS> rlist=service.queryUserReply(userid);
		map.put("total", page.getTotal());	//获取总条数
		map.put("rlist", rlist);
		System.out.println(map);
		return map;
	}
	
	
}
